const mongoose = require("../utils/db");

const userSchema = new mongoose.Schema({
  username: String,
  password: String,
  avatar: String
});
var User = mongoose.model("users", userSchema);

// 插入一个用户
const insertUser = userinfo => {
  const user = new User(userinfo);
  return new Promise((resolve, reject) => {
    user.save((err, doc) => {
      if (err) reject(err);
      else resolve(doc);
    });
  });
};

// 查找用户
const isUserExist = userinfo => {
  return new Promise((resolve, reject) => {
    User.find(userinfo, (err, docs) => {
      if (err) reject(err);
      else resolve(docs);
    });
  });
};

module.exports = {
  insertUser,
  isUserExist
};
